﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_ProcessStepDoing")]
    public class P_ProcessStepDoing : IEntity<P_ProcessStepDoing>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public int Type { get; set; }
        public int ProjectID { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(50)]
        public string StepName { get; set; }
        public int CurrentProcessID { get; set; }
        public int PreviousProcessID { get; set; }
        public int NextProcessID { get; set; }

        public override P_ProcessStepDoing Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_ProcessStepDoing Find(System.Linq.Expressions.Expression<Func<P_ProcessStepDoing, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.ProcessStepDoing.FirstOrDefault(where);
                if (item != null)
                {
                    this.CurrentProcessID = item.CurrentProcessID;
                    this.ID = item.ID;
                    this.NextProcessID = item.NextProcessID;
                    this.PreviousProcessID = item.PreviousProcessID;
                    this.ProjectID = item.ProjectID;
                    this.Type = item.Type; 
                    this.StepName = item.StepName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_ProcessStepDoing, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.ProcessStepDoing.Any(where);
            }
        }
    }
}
